
from langchain_core.tools import tool
import math

@tool
def calculate(expression: str) -> dict:
    """Evaluate a mathematical expression."""
    try:
        # Safe evaluation of mathematical expressions
        allowed_names = {
            k: v for k, v in math.__dict__.items() if not k.startswith("__")
        }
        result = eval(expression, {"__builtins__": {}}, allowed_names)
        return {'success': True, 'result': result}
    except Exception as e:
        return {'success': False, 'result': str(e)}